import pymysql.cursors
from conf.readConfig import ReadConfig
import json
from tool_class.marketing_plan_stepthree import MarketingPlanStepthree
import os



class Conne_DB():
    def conne_db(self, sql):
        host = ReadConfig().get_db("host")
        port = ReadConfig().get_db("port")
        user = ReadConfig().get_db("user")
        password = ReadConfig().get_db("password")
        db = ReadConfig().get_db("db")
        # print(host,port,user,password,db,sql)

        connect = pymysql.Connect(
            host=host,
            port=int(port),
            user=user,
            password=password,
            db=db,
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor
        )

        cursor = connect.cursor()
        cursor.execute(sql)
        data = cursor.fetchall()
        # print(data)

        cursor = connect.cursor()
        cursor.execute(sql)
        return data


if __name__ == "__main__":
    # plan_id = MarketingPlanStepthree().marketing_plan_step_three()
    # sql = "select `key` from `key` where key_batch_id in  ( select id from key_batch where plan_id= %d) and status = 1 limit 0,1" % plan_id
    sql1 = "select data from market_approval where approval_id = 4031"
    key = Conne_DB().conne_db(sql1)
    print(key)
    print(key[0]["data"])
    data = json.loads(key[0]["data"])
    print(data["info"]["title"])
    print(type(data))
